package com.componentmanager

import com.modulemanager.page.PageInterface

class ComponentManager {
    companion object {
        val instance: ComponentManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ComponentManager()
        }
    }

    private val componentBuilders: MutableMap<String, ComponentInterface.Builder> = mutableMapOf()

    fun register(builder: ComponentInterface.Builder) {
        componentBuilders[builder.type] = builder
    }

    fun component(
        page: PageInterface,
        type: String,
        id: String?,
        value: Any?,
    ): ComponentInterface? {
        val componentBuilder: ComponentInterface.Builder = componentBuilders[type] ?: return null

        return componentBuilder.build(page, id, value)
    }
}